package com.qire.manhua.model.viewModel.impl;

import android.view.View;

import com.qire.antscore.annotation.PropertyObserve;
import com.qire.common.basic.StringUtil;
import com.qire.common.collect.ArrayUtil;
import com.qire.common.support.base.BaseViewModel;
import com.qire.common.support.base.IViewModel;
import com.qire.common.utils.ToastHelper;
import com.qire.common.widget.snackbar.Prompt;
import com.qire.manhua.BR;
import com.qire.manhua.R;
import com.qire.manhua.dal.service.TeensModeService;
import com.qire.manhua.model.enumeration.TeensModeScene;

import androidx.lifecycle.MutableLiveData;

public class TeensModifyPasswordViewModel extends BaseViewModel implements IViewModel {

    private static final int OldPWDPanelID = 1;
    private static final int NewPWDPanelOneID = 2;
    private static final int NewPWDPanelTwoID = 3;

    private final PanelSate OldPasswordPanel = new PanelSate(OldPWDPanelID, "输入当前密码", "下一步", View.VISIBLE);
    private final PanelSate NewPasswordPanelOne = new PanelSate(NewPWDPanelOneID, "输入新密码", "下一步", View.GONE);
    private final PanelSate NewPasswordPanelTwo = new PanelSate(NewPWDPanelTwoID, "确认新密码", "确认修改", View.GONE);

    private final TeensModeService toggleService = new TeensModeService();

    private PanelSate panelState = OldPasswordPanel;

    private String passwordOld;
    private String passwordNewOne;
    private String passwordNewTwo;

    @Override
    public int getVariableId() {
        return BR.teensModifyPasswordViewModel;
    }



    // region    #业务
    public void nextAction() {
        switch (panelState.panelId) {
            case OldPWDPanelID: {
                passwordOld = panelState.passwordCode();
                toggleService.verifyPassword(passwordOld, TeensModeScene.ModifyPassword.sceneLabel);
                break;
            }
            case NewPWDPanelOneID: {
                passwordNewOne = panelState.passwordCode();
                showNewPasswordPanelTwo();
                break;
            }
            case NewPWDPanelTwoID: {
                passwordNewTwo = panelState.passwordCode();
                if(passwordNewTwo.equals(passwordNewOne)) {
                    toggleService.modifyPassword(passwordOld, passwordNewOne);
                } else {
                    ToastHelper.showSnackBar("输入密码不一致", Prompt.ERROR);
                }
                break;
            }
        }
    }

    public boolean canBack() {
        switch (panelState.panelId) {
            case NewPWDPanelTwoID: {
                showNewPasswordPanelOne();
                return true;
            }
            case NewPWDPanelOneID: {
                showOldPasswordPanel();
                return true;
            }
            case OldPWDPanelID:
            default:
                return false;
        }
    }

    // endregion #业务

    // region    #UI操作
    public void showOldPasswordPanel() {
        panelState = OldPasswordPanel;
        resetPasswordCodeUI();
        refreshUI();
    }
    public void showNewPasswordPanelOne() {
        panelState = NewPasswordPanelOne;
        resetPasswordCodeUI();
        refreshUI();
    }
    public void showNewPasswordPanelTwo() {
        panelState = NewPasswordPanelTwo;
        resetPasswordCodeUI();
        refreshUI();
    }

    private void refreshUI() {
        set("title", panelState.title);
        set("nextBtnText", panelState.nextBtnText);
        set("forgetPasswordTipsVisible", panelState.forgetPasswordTipsVisible);
        refreshNextBtnBgRid(panelState.isFull());
    }

    private void refreshNextBtnBgRid(boolean clickable) {
        set("nextBtnClickable", clickable);
        set("nextBtnBgRid", clickable ? R.drawable.app_teens_button_yellow : R.drawable.app_teens_button_gray);
    }

    private void resetPasswordCodeUI() {
        set("passwordCode", new CharSequence[] {"", "", "", ""});
        set("codeFocusIndex", 0);
    }

    @PropertyObserve(name = "passwordCode")
    public void onPasswordCodeChange(CharSequence[] passwordCode) {
        ArrayUtil.replace(passwordCode, panelState.passwordCodes);
        refreshNextBtnBgRid(panelState.isFull());
    }

    // endregion #UI操作

    // region    #UI绑定
    public MutableLiveData<CharSequence> getTitle() {
        return get("title");
    }

    public MutableLiveData<CharSequence[]> getPasswordCode() {
        return get("passwordCode");
    }

    public MutableLiveData<Integer> getCodeFocusIndex() {
        return get("codeFocusIndex");
    }

    public MutableLiveData<Boolean> getNextBtnClickable() {
        return get("nextBtnClickable");
    }

    public MutableLiveData<CharSequence> getNextBtnText() {
        return get("nextBtnText");
    }

    public MutableLiveData<Integer> getNextBtnBgRid() {
        return get("nextBtnBgRid");
    }

    public MutableLiveData<Integer> getForgetPasswordTipsVisible() {
        return get("forgetPasswordTipsVisible");
    }
    // endregion #UI绑定

    private class PanelSate {

        final int panelId;
        final String title;
        final String nextBtnText;
        final int forgetPasswordTipsVisible;

        final String[] passwordCodes = new String[4];

        PanelSate(int panelId, String title, String nextBtnText, int forgetPasswordTipsVisible) {
            this.panelId = panelId;
            this.title = title;
            this.nextBtnText = nextBtnText;
            this.forgetPasswordTipsVisible = forgetPasswordTipsVisible;
        }

        public boolean isFull() {
            for(String code : passwordCodes) {
                if(StringUtil.isEmpty(code)) {
                    return false;
                }
            }
            return true;
        }

        public String passwordCode() {
            StringBuilder passwordCodeBuilder = new StringBuilder(4);
            for(String code : passwordCodes) {
                if(StringUtil.notEmpty(code)) {
                    passwordCodeBuilder.append(code.charAt(0));
                }
            }
            return passwordCodeBuilder.toString();
        }
    }
}
